<?php
#langito installation module
#get rid off notices
error_reporting(E_ALL ^ E_NOTICE);

#session start
session_name('langito_install');
session_start();

include('includes/functions/std.php');
include('includes/functions/txt.php');
include('includes/classes/template.class.php');		

#files that should be chmodded 777 for proper installation
$chmod777=array('upload/', 'tpl_cache/', 'sql_cache/');

#files that should be deleted after instalation
$delete_files=array('install/', 'install.php');

#install files/configuration
$sql_file_url['latin2']='install/latin_sql.txt';
$sql_file_url['utf8']='install/utf_sql.txt';
$file_url['utf8']='install/utf_test.txt';
$file_url['latin2']='install/latin_test.txt';
$mysql_ver=array("5"=>"utf8", "3"=>"latin2", "4"=>"latin2");
$conf_file_url='install/conf.php'; 
#

#main script

#accept language
if(!$_SESSION['ACCEPT_LANGUAGE']) {
	$_SESSION['ACCEPT_LANGUAGE']=explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
	foreach($_SESSION['ACCEPT_LANGUAGE'] as $k=>$v) {
		$_SESSION['ACCEPT_LANGUAGE'][$k]=explode(";", $v);
		$_SESSION['ACCEPT_LANGUAGE'][$k]=$_SESSION['ACCEPT_LANGUAGE'][$k][0];
	}
}
#

#let default_charset be set by client
ini_set("default_charset", ""); 
#

#include supported language
if(!$_SESSION['lang_short']) {
	foreach($_SESSION['ACCEPT_LANGUAGE'] as $lng) {
		if(file_exists('install/lang/'.$lng.'.php')) {
			$_SESSION['lang_short']=$lng;
			break;
		}
	}
}
if(!$_SESSION['lang_short']) $_SESSION['lang_short']='en';
include('install/lang/'.$_SESSION['lang_short'].'.php');
#

#get other lang_options
$dir='install/lang/';
$lng_opt=array();
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
			$name=explode('.', $file);
			if($file==$_SESSION['lang_short'].'.php' && !is_dir($dir.$file))
				$lng_opt[]='<option value="'.$name[0].'">'.strtoupper($name[0]).'</option>';
			else
				$lng_opt[]='<option value="'.$name[0].'" checked="true">'.strtoupper($name[0]).'</option>';
        }
        closedir($dh);
    }
}
#

#if already installed but still install/ or install.php exists
if((file_exists('install/') or file_exists('install.php')) && file_exists('includes/conf.php')) {
	$_POST['wyslane']=2;
}

if($_POST['accept'])
	$_SESSION['accept'] = $_POST['accept'];

if(!$_SESSION['accept'])
{
	$content .= '<form action="install.php" method="post">
		<table align="center" width="100%" border="0" class="tabelka2">
		<tr>
		<td class="row_3" colspan="2"><iframe frameborder="0" src="'.$lang['lang_license'].'" width="100%" height="400"></iframe></td>
		</tr>
		</table>
		<br />
		<table align="center" width="100%" border="0">
		<tr>
		<td><input type="checkbox" name="accept" value="tak" class="button" /></td>
		<td><b>'.$lang['install_accept'].'</b></td>
		</tr>
		<tr>
		<td></td>
		<td><input type="submit" class="button" name="send" value="dalej" /></td>
		</tr>
		</table>
		';
}
elseif(!$_POST['wyslane'])
{  
	foreach($chmod777 as $check)
	{
		$exp=explode('.', $check);
		if(count($exp)==2)
		{
			if(!@fopen($check, 'a+')) 
			{ 
				$err=true;
				$dirs.='<br />'.$check;
			}
		}
		elseif(!ereg('777', substr(sprintf('%o', fileperms($check)), -4)))
		{
			$err=true;
			$dirs.='<br />'.$check;	  
		}
	}

	if($err)
	{
		$content=string_template($lang['install_error'][4], array('dirs'=>$dirs));
		$description=$lang['install_error_text'];
	}
	else
	{

		$content='
			<form action="install.php" method="post">
			<input type="hidden" name="wyslane" value="1">
			<table align="center" width="100%" border="0" class="tabelka2">
			<tr>
			<td class="row_3" colspan="2">'.$lang['mysql_text'].'</td>
			</tr>
			<tr>
			<td class="error" colspan="2" align="center">'.$lang['install_error'][$_GET['error']].'</td>
			</tr>
			<tr>
			<td class="row_1" align="center">'.$lang['mysql_host'].'</td>
			<td class="row_1" align="left"><input type="text" maxlength="150" name="mysql_host" class="button" size="20" value="localhost" /></td>
			</tr>
			<tr>
			<td class="row_2" align="center">'.$lang['mysql_user'].'</td>
			<td class="row_2" align="left"><input type="text" maxlength="150" name="mysql_user" class="button" size="20" /></td>
			</tr>
			<tr>
			<td class="row_1" align="center">'.$lang['mysql_db_name'].'</td>
			<td class="row_1" align="left"><input type="text" maxlength="150" name="mysql_database" class="button" size="20" /></td>
			</tr>
			<tr>
			<td class="row_2" align="center">'.$lang['mysql_password'].'</td>
			<td class="row_2" align="left"><input type="password" maxlength="150" name="mysql_pass" class="button" size="20" /></td>
			</tr>
			<tr>
			<td class="row_1" align="center">'.$lang['mysql_coding'].'</td>
			<td class="row_1" align="left"><select name="mysql_coding" class="button"><option value="utf8">utf8</option></td>
			</tr>
			<tr>
			<td class="row_2" align="center">'.$lang['mysql_prefix'].'</td>
			<td class="row_2" align="left"><input type="text" maxlength="15" name="mysql_prefix" value="langito_" class="button" size="15" /></td>
			</tr>
			<tr>
			<td class="row_3" colspan="2">'.$lang['admin_text'].'</td>
			</tr>
			<tr>
			<td class="row_1" align="center">'.$lang['admin_login'].'</td>
			<td class="row_1" align="left"><input type="text" maxlength="50" name="admin_login" value="admin" class="button" size="20"></td>
			</tr>
			<tr>
			<td class="row_2" align="center">'.$lang['admin_password'].'</td>
			<td class="row_2" align="left"><input type="password" maxlength="30" name="admin_pass1" class="button" size="20"></td>
			</tr>
			<tr>
			<td class="row_1" align="center">'.$lang['admin_repeat_password'].'</td>
			<td class="row_1" align="left"><input type="password" maxlength="30" name="admin_pass2" class="button" size="20"></td>
			</tr>
			<tr>
			<td></td>
			<td><input type="submit" value="'.$lang['next'].'" class="button" /></td>
			</tr>
			<tr>
			<td></td>
			<td>'.$install_txt.'</form>
			</tr>
			</table></form>
			';
	}
}

if($_POST['wyslane']=='1')
{
	$mysql_connect=@mysql_connect($_POST['mysql_host'], $_POST['mysql_user'], $_POST['mysql_pass']);
	$mysql_select_db=@mysql_select_db($_POST['mysql_database']);

	#check database
	if(!$mysql_connect)
	{
		redirect('install.php?error=1');
		exit;
	}

	if(!$mysql_select_db)
	{
		redirect('install.php?error=2');
		exit;
	}
	#
	
	#if already installed
	if(file_exists('includes/conf.php'))
	{
		redirect('install.php?error=6');
		exit;
	}
	#

	#checking admin password
	if($_POST['admin_pass1']!=$_POST['admin_pass2'] or $_POST['admin_pass1']=='' or $_POST['admin_pass2']=='')
	{
		redirect('install.php?error=3');
		exit;
	}
	#

	#mysql preferences
	$conf['host'] = $_POST['mysql_host'];
	$conf['database'] = $_POST['mysql_database'];
	$conf['user'] = $_POST['mysql_user'];
	$conf['pass'] = $_POST['mysql_pass'];
	$conf['prefix'] = $_POST['mysql_prefix'];
	$conf['mysql_character_set']=$_POST['mysql_coding'];
	#

	$sql_file=@fopen ($sql_file_url[$_POST['mysql_coding']], "r");
	$conf_file=@fopen ($conf_file_url, "r");	
	
	#conf file check
	if(!$conf_file && !$sql_file)
	{
		redirect('install.php?error=4');
		exit;
	}
	#
	
	#conf file
	$conf_string=fread($conf_file, filesize($conf_file_url));
	fclose($conf_file);
	$conf_string=string_template($conf_string, array('mysql_host' => $_POST['mysql_host'], 'mysql_user' => $_POST['mysql_user'], 'mysql_database' => $_POST['mysql_database'], 'mysql_pass' => $_POST['mysql_pass'], 'mysql_prefix'=>$_POST['mysql_prefix'], 'mysql_coding'=>$_POST['mysql_coding']));

	$conf_file=@fopen ('includes/conf.php', "w+");
	$conf_file=@fwrite($conf_file, $conf_string);
	#

	#sql read dump
	$read_sql_file = fread($sql_file, filesize($sql_file_url[$_POST['mysql_coding']]));
	fclose($sql_file);
	
	$query = explode(";\n", $read_sql_file);
	if(count($query)==1)
		$query = explode(";\r\n", $read_sql_file);
	
	#use mysql class
	include_once('includes/classes/mysql.db.class.php');
	$db=new dbquery;
	#
	
	#mysql set mysql_cahrset
	$sqlCharset=' CHARACTER SET '.$conf['mysql_character_set'];

	#run the query
	foreach($query as $q) {
		if($q){
			$q = string_template($q, array('mysql_prefix'=>$_POST['mysql_prefix'], 'admin_pass'=>md5($_POST['admin_pass1']), 'admin_login'=>$_POST['admin_login'], 'reg_date'=>date("Y-m-d"), 'mysql_charset'=>$sqlCharset));
			$db->query($q);
		}
	}
	
	#success!
	$_POST['wyslane']=2;
}

#display success notification
if($_POST['wyslane']=='2') {
	$content=$lang['install_succesfull_text'];
	$description=$lang['install_succesfull'];
}

//put $content to proper theme/file
$content = string_template(read_file('install/middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $description));

//site_generation_time
$GLOBALS['time_end'] = microtime(1);
$site_generation_time=abs(round($GLOBALS['time_end']-$GLOBALS['time_start'], 3));
//

//creating template
$tpl=new tpl;
//header
$tpl->create('install/overall_header.php', array('title' => $GLOBALS['lang']['global']['site_title'], 'description' => $GLOBALS['lang']['global']['site_description'], 'keywords' => $GLOBALS['lang']['global']['site_keywords'], 'theme_path'=>$GLOBALS['theme_path'], 'menu_top' => $menu_top));
//body
$tpl->create('install/body_no_right.php', array('theme_path' => $GLOBALS['theme_path'], 'menu_top' => $menu_top, 'menu_left' => $menu_left, 'menu_right' => $menu_right, 'content' => $content));
//footer
$tpl->create('install/overall_footer.php', array('site_generation_time' => string_template($GLOBALS['lang']['global']['site_generation_time'], array('site_generation_time'=>$site_generation_time, 'queries'=>$db->query_count())), 'theme_path'=>$GLOBALS['theme_path'], 'copyright' => $GLOBALS['lang']['global']['site_copyright'], 'queries'=>$db->query_count()));
//parse
$tpl->parse();

?>
